package com.futvan.z.erp.erp_search_engines.job;


import com.futvan.z.erp.erp_search_engines_info.erp_search_engines_info;
import com.futvan.z.framework.util.HttpUtil;
import com.futvan.z.job.ContentBean;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.futvan.z.framework.core.z;

public class job_360_m implements Job {

    public void execute(JobExecutionContext context) throws JobExecutionException {
//关键字
        String keyinfo = String.valueOf(context.getJobDetail().getJobDataMap().get("keyinfo"));
        //模板ID
        String zid = String.valueOf(context.getJobDetail().getJobDataMap().get("zid"));
        //执行采集任务
        try {
            getContent(keyinfo, 10, null, 0, zid);
        } catch (Exception e) {
            throw new JobExecutionException("采购360手机端舆情信息出错|" + e.getMessage());
        }
    }

    /**
     * 获取360手机舆情信息
     *
     * @param search
     * @param pages
     * @param ip
     * @param port
     * @param zid
     * @throws Exception
     */
    public static void getContent(String search, int pages, String ip, int port, String zid) throws Exception {
//        for (int i = 1; i <= 10; i++) {
        // System.out.println(i);
        String url = "https://m.so.com/s?q=%E7%BE%8E%E7%BD%97%E5%9B%BD%E9%99%85";
        String html = "";
        try {
            html = HttpUtil.doGet(url, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Document doc = Jsoup.parse(html);
        Elements list = doc.getElementsByClass("g-card");

        for (Element link : list) {

            ContentBean bean = new ContentBean();

            //标题
            if (null != link.getElementsByTag("h3").text()) {
                if (!"".equals(link.getElementsByTag("h3").text())) {
                    bean.setTitle(link.getElementsByTag("h3").text());
                } else {
                    Elements Title = link.select("h3[class=res-title]");
                    for (Element title1 : Title) {
                        String titles = title1.select("a").attr("em");
                        bean.setTitle(titles);
                    }
                }
            }

            // 判读数据库中是否存在
            int num = z.getSqlSession().selectOne("selectoneint", "SELECT COUNT(*) FROM erp_search_engines_info WHERE title = '" + bean.getTitle() + "' AND search_engines_id = '" + zid + "'");
            if (num == 0) {
                //真实链接
                String s = link.select("a[class=alink]").attr("href");
                bean.setSrc(s);

                //来源
                Elements From = link.getElementsByClass("res-supplement");
                for (Element from : From) {
                    String comeFrom = from.select("cite").text();
                    bean.setFrom(comeFrom);
                }

                if (z.isNotNull(bean.getSrc())) {
                    String ipinfo = "";
                    if (z.isNotNull(ip) && port > 0) {
                        ipinfo = "|代理IP：" + ip + "|端口：" + port;
                    }
                    //z.Log("【第"+i+"页】"+ipinfo+"|标题："+bean.getTitle()+"|来源："+bean.getSrc());

                    //保存网站信息
                    erp_search_engines_info info = new erp_search_engines_info();
                    info.setZid(z.newZid("erp_search_engines_info"));
                    info.setTitle(bean.getTitle());
                    info.setFrom_website(bean.getFrom());
                    info.setUrl(bean.getSrc());
                    //info.setSseq("出现在第"+page_num+"页");
                    info.setSearch_engines_id(zid);
                    z.getSqlSession().insert("erp_search_engines_info_insert", info);
                }
            }
        }
    }
}
